/*
 * ChooseProjectJPanel.java
 *
 * Created on April 27, 2006, 10:03 AM
 */

package org.toryt_II.main.swing;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import javax.swing.JFileChooser;
import org.toryt_II.testmodel.ClassInspectorTestModel;
import org.toryt_II.testmodel.ClassMutatorTestModel;
import org.toryt_II.testmodel.ClassTestModel;
import org.toryt_II.testmodel.ConstructorTestModel;
import org.toryt_II.testmodel.DefaultTestModelFactory;
import org.toryt_II.testmodel.InnerClassTestModel;
import org.toryt_II.testmodel.InstanceInspectorTestModel;
import org.toryt_II.testmodel.InstanceMutatorTestModel;
import org.toryt_II.testmodel.MethodTestModel;
import org.toryt_II.testmodel.PackageTestModel;
import org.toryt_II.testmodel.ProjectTestModel;
import org.toryt_II.testmodel.TestModel;
import org.toryt_II.testmodel.TestModelFactory;

/**
 *
 * @author  jand
 */
public class ChooseProjectJPanel extends javax.swing.JPanel {
  
  /** Creates new form ChooseProjectJPanel */
  public ChooseProjectJPanel() {
    initComponents();
  }
  
  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    $projectNameLabel = new javax.swing.JLabel();
    $projectNameField = new javax.swing.JTextField();
    $classDirectoryPathLabel = new javax.swing.JLabel();
    $classDirectoryPathField = new javax.swing.JTextField();
    $classDirectoryFileChooserButton = new javax.swing.JButton();
    $testButtonPanel = new javax.swing.JPanel();
    $startTestButton = new javax.swing.JButton();
    $stopTestButton = new javax.swing.JButton();

    setLayout(new java.awt.GridBagLayout());

    setBorder(javax.swing.BorderFactory.createTitledBorder(""));
    $projectNameLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    $projectNameLabel.setLabelFor($projectNameField);
    $projectNameLabel.setText("Project Name");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    add($projectNameLabel, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    add($projectNameField, gridBagConstraints);

    $classDirectoryPathLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    $classDirectoryPathLabel.setLabelFor($classDirectoryPathField);
    $classDirectoryPathLabel.setText("Project Class Directory");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    add($classDirectoryPathLabel, gridBagConstraints);

    $classDirectoryPathField.setEnabled(false);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.gridwidth = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    add($classDirectoryPathField, gridBagConstraints);

    $classDirectoryFileChooserButton.setFont(new java.awt.Font("Lucida Grande", 0, 10));
    $classDirectoryFileChooserButton.setText("...");
    $classDirectoryFileChooserButton.setToolTipText("Select the Class Directory");
    $classDirectoryFileChooserButton.setMargin(new java.awt.Insets(1, 1, 1, 1));
    $classDirectoryFileChooserButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        $classDirectoryFileChooserButtonActionPerformed(evt);
      }
    });

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 3;
    gridBagConstraints.gridy = 1;
    add($classDirectoryFileChooserButton, gridBagConstraints);

    $startTestButton.setText("Start Test");
    $startTestButton.setEnabled(false);
    $startTestButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        $startTestButtonActionPerformed(evt);
      }
    });

    $testButtonPanel.add($startTestButton);

    $stopTestButton.setText("Stop Test");
    $stopTestButton.setEnabled(false);
    $testButtonPanel.add($stopTestButton);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.gridwidth = 4;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    add($testButtonPanel, gridBagConstraints);

  }// </editor-fold>//GEN-END:initComponents

  private void $classDirectoryFileChooserButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_$classDirectoryFileChooserButtonActionPerformed
    File selectionDir;
    if ($classDirectory != null) {
      selectionDir = $classDirectory.getParentFile();
      if (selectionDir == null) {
        // we are at the root; take the root itself
        selectionDir = $classDirectory;
      }
    }
    else {
      selectionDir = new File(System.getProperty("user.home"));
    }
    JFileChooser fc = new JFileChooser(selectionDir);
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int result = fc.showDialog(this, "Select Class Directory");
    if (result == JFileChooser.APPROVE_OPTION) {
      $classDirectory = fc.getSelectedFile();
      $classDirectoryPathField.setText($classDirectory.getPath());
      createTestModel();
    }
    if (($classDirectory != null) &&
        (($projectNameField.getText() != null) && (! $projectNameField.getText().equals("")))) {
      $startTestButton.setEnabled(true);
      $stopTestButton.setEnabled(false);
    }
  }//GEN-LAST:event_$classDirectoryFileChooserButtonActionPerformed

  private void $startTestButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_$startTestButtonActionPerformed
    assert $testModel != null;
    $testModel.
  }//GEN-LAST:event_$startTestButtonActionPerformed
  
  
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton $classDirectoryFileChooserButton;
  private javax.swing.JTextField $classDirectoryPathField;
  private javax.swing.JLabel $classDirectoryPathLabel;
  private javax.swing.JTextField $projectNameField;
  private javax.swing.JLabel $projectNameLabel;
  private javax.swing.JButton $startTestButton;
  private javax.swing.JButton $stopTestButton;
  private javax.swing.JPanel $testButtonPanel;
  // End of variables declaration//GEN-END:variables
  
  private File $classDirectory = null;
  private TestModel $testModel = null;
  private final TestModelFactory $testModelFactory = new DefaultTestModelFactory();
  
  private void createTestModel() {
    String projectName = $projectNameField.getText();
    if (($classDirectory != null) &&
        (projectName != null) && (! projectName.equals(""))) {
      $testModel = $testModelFactory.createProjectTestModel($classDirectory, projectName);
      $startTestButton.setEnabled(true);
    }
    else {
      $startTestButton.setEnabled(false);
    }
    $stopTestButton.setEnabled(false);
  }
  
}
